package com.api.nanc.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.nanc.util.BaseUtil;
import com.api.nanc.util.SqlUtil;
import com.api.shihua.util.LogTool;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.MainTableInfo;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;
import weaver.soa.workflow.request.RequestService;
import weaver.workflow.action.BaseAction;
import weaver.workflow.workflow.WorkflowVersion;

import java.util.*;

/*
       触发子流程
 */
public class CreateChildWFFw extends BaseAction {
    private final static LogTool loghr = new LogTool("/log/hr/",false);
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid=requestInfo.getRequestid();
        JSONObject json=null;
        JSONArray jsonaLL=new JSONArray();
        loghr.writeLog("CreateChildWF"+requestid);

        WorkflowVersion WorkflowVersion=new WorkflowVersion("21");
        String wfid= WorkflowVersion.getActiveVersionWFID();
        JSONObject wfjson=BaseUtil.getRequestInfoById(requestid);
        String  formid=wfjson.getString("formid");
        RecordSet re=new RecordSet();
        RecordSet re1=new RecordSet();
        String sql="select a.*,b.id detailid,b.cdbm,b.jsr,b.zlcid,c.requestname from formtable_main_"+formid+" a,formtable_main_"+formid+"_dt1 b,workflow_requestbase c where a.id=b.mainid and a.requestid=c.requestid and a.requestid='"+requestid+"'";
        loghr.writeLog("CreateChildWF-->"+sql);
        re.execute(sql);
        while(re.next()){
            json=new JSONObject();
            String zlcid=Util.null2String(re.getString("zlcid")) ;//子流程id
            String sfxyhf=Util.null2String(re.getString("sfxyhf")) ;//是否需要回复
            if(!"".equals(zlcid)||"1".equals(sfxyhf)){
                continue;
            }
            String requestname= Util.null2String(re.getString("requestname")) ;//请求标题
            String yfrq= Util.null2String(re.getString("yfrq")) ;//发布日期
            String fj= Util.null2String(re.getString("fj")) ;//附件
            String ngr= Util.null2String(re.getString("ngr")) ;//发布人员
            String ngdw= Util.null2String(re.getString("ngdw")) ;//发布单位
            String ngbm= Util.null2String(re.getString("ngbm")) ;//发布部门
            String nggw= Util.null2String(re.getString("nggw")) ;//发布岗位
            String xglc= Util.null2String(re.getString("xglc")) ;//相关流程
            String zw= Util.null2String(re.getString("zw")) ;//正文
            String cdbm= Util.null2String(re.getString("cdbm")) ;//被征求部门
            String jsr= Util.null2String(re.getString("jsr")) ;//接收人
            if("".equals(jsr)){
                continue;
            }
            String yqhfrq= Util.null2String(re.getString("yqhfrq")) ;//回复直接日期
            String detailid= Util.null2String(re.getString("detailid")) ;//明细ID
            json.put("requestname",requestname);//请求标题
            json.put("ngrq",yfrq);//发布日期
            json.put("fj",fj);//附件
            json.put("ngr",ngr);//发布人员
            json.put("ngdw",ngdw);//发布单位
            json.put("ngbm",ngbm);//发布部门
            json.put("nggw",nggw);//发布岗位
            json.put("yfrq",yfrq);//发布日期
            json.put("xglc",xglc);//相关流程
            json.put("zw",zw);//正文
            json.put("fkdw",cdbm);//被征求部门
            json.put("zs",jsr);//zs
            json.put("hfjzrq",yqhfrq);//回复直接日期
            json.put("detailid",detailid);//
            json.put("mainrequestid",requestid);//
            jsonaLL.add(json);
            //int chiidReq=getRequestid(json,wfid,formid);


        }
        if(jsonaLL.size()>0){
            for(int i=0;i<jsonaLL.size();i++){
                JSONObject WFJSON=jsonaLL.getJSONObject(i);
                int chiidReq=getRequestid(WFJSON,wfid,formid);
                loghr.writeLog("chiidReq"+chiidReq);
                if(chiidReq>0){
                    //创建成功
                    loghr.writeLog("chiidReq"+"update formtable_main_"+formid+"_dt1 set zlcid='"+chiidReq+"' where id="+json.getString("detailid"));
                    re1.execute("update formtable_main_"+formid+"_dt1 set zlcid='"+chiidReq+"' where id="+json.getString("detailid"));
                }else{
                    requestInfo.getRequestManager().setMessageid("10001");
                    requestInfo.getRequestManager().setMessagecontent("子流程创建失败!");
                    return Action.FAILURE_AND_CONTINUE;
                }
            }
        }
        return super.execute(requestInfo);
    }
    public int getRequestid(JSONObject json,String workflowid,String  formid){
        int reslutRequestid=-1;
        User user=new   User(1);//用管理员创建
        try{
            RequestInfo ri=new RequestInfo();
            //添加主表数据
            MainTableInfo mti = new MainTableInfo();
            //读取创建人
            ri.setCreatorid(1+"");//表单创建人id(申请人)
            //流程ID
            ri.setWorkflowid(workflowid);//根据问题类型查找对应的流程ID
            ri.setIsNextFlow("1");
            //流程标题  Title+"-"+SubCompanyname+"-"+
            String title=json.getString("requestname");
            ri.setDescription(title);
            //设置流程字段
            ri.setMainTableInfo(mti);
            Property[] p = new Property[0];

            p = Arrays.copyOf(p, (p.length+1));
            p[p.length-1] = new Property();
            p[p.length-1].setName("ngrq");
            p[p.length-1].setValue(json.getString("ngrq")+"");
            loghr.writeLog("cjr-->"+Util.null2String(json.getString("ngrq")));

            p = Arrays.copyOf(p, (p.length+1));
            p[p.length-1] = new Property();
            p[p.length-1].setName("fj");
            p[p.length-1].setValue(json.getString("fj")+"");
            loghr.writeLog("fj-->"+Util.null2String(json.getString("fj")));

            p = Arrays.copyOf(p, (p.length+1));
            p[p.length-1] = new Property();
            p[p.length-1].setName("ngr");
            p[p.length-1].setValue(json.getString("ngr")+"");
            loghr.writeLog("ngr-->"+Util.null2String(json.getString("ngr")));

            p = Arrays.copyOf(p, (p.length+1));
            p[p.length-1] = new Property();
            p[p.length-1].setName("ngdw");
            p[p.length-1].setValue(json.getString("ngdw")+"");
            loghr.writeLog("ngdw-->"+Util.null2String(json.getString("ngdw")));

            p = Arrays.copyOf(p, (p.length+1));
            p[p.length-1] = new Property();
            p[p.length-1].setName("ngbm");
            p[p.length-1].setValue(json.getString("ngbm")+"");
            loghr.writeLog("ngbm-->"+Util.null2String(json.getString("ngbm")));

            p = Arrays.copyOf(p, (p.length+1));
            p[p.length-1] = new Property();
            p[p.length-1].setName("nggw");
            p[p.length-1].setValue(json.getString("nggw")+"");
            loghr.writeLog("nggw-->"+Util.null2String(json.getString("nggw")));

            p = Arrays.copyOf(p, (p.length+1));
            p[p.length-1] = new Property();
            p[p.length-1].setName("yfrq");
            p[p.length-1].setValue(json.getString("yfrq")+"");
            loghr.writeLog("yfrq-->"+Util.null2String(json.getString("yfrq")));

            p = Arrays.copyOf(p, (p.length+1));
            p[p.length-1] = new Property();
            p[p.length-1].setName("xglc");
            p[p.length-1].setValue(json.getString("xglc")+"");
            loghr.writeLog("xglc-->"+Util.null2String(json.getString("xglc")));

            p = Arrays.copyOf(p, (p.length+1));
            p[p.length-1] = new Property();
            p[p.length-1].setName("xglc");
            p[p.length-1].setValue(json.getString("xglc")+"");
            loghr.writeLog("xglc-->"+Util.null2String(json.getString("xglc")));

            p = Arrays.copyOf(p, (p.length+1));
            p[p.length-1] = new Property();
            p[p.length-1].setName("zs");
            p[p.length-1].setValue(json.getString("zs")+"");
            loghr.writeLog("zs-->"+Util.null2String(json.getString("zs")));

            //p = Arrays.copyOf(p, (p.length+1));
            //p[p.length-1] = new Property();
            //p[p.length-1].setName("zw");
            //p[p.length-1].setValue(json.getString("zw")+"");
            //loghr.writeLog("zw-->"+Util.null2String(json.getString("zw")));

            p = Arrays.copyOf(p, (p.length+1));
            p[p.length-1] = new Property();
            p[p.length-1].setName("fkdw");
            p[p.length-1].setValue(json.getString("fkdw")+"");
            loghr.writeLog("fkdw-->"+Util.null2String(json.getString("fkdw")));

            p = Arrays.copyOf(p, (p.length+1));
            p[p.length-1] = new Property();
            p[p.length-1].setName("hfjzrq");
            p[p.length-1].setValue(json.getString("hfjzrq")+"");
            loghr.writeLog("hfjzrq-->"+Util.null2String(json.getString("hfjzrq")));



            p = Arrays.copyOf(p, (p.length+1));
            p[p.length-1] = new Property();
            p[p.length-1].setName("detailid");
            p[p.length-1].setValue(json.getString("detailid")+"");
            loghr.writeLog("detailid-->"+Util.null2String(json.getString("detailid")));

            p = Arrays.copyOf(p, (p.length+1));
            p[p.length-1] = new Property();
            p[p.length-1].setName("mainrequestid");
            p[p.length-1].setValue(json.getString("mainrequestid")+"");
            loghr.writeLog("mainrequestid-->"+Util.null2String(json.getString("mainrequestid")));

            mti.setProperty(p);
            RequestService service = new RequestService();
            reslutRequestid=Util.getIntValue(service.createRequest(ri),-1) ;
            if(reslutRequestid>0){
                updZw("formtable_main_27",json.getString("zw")+"",reslutRequestid+"");
            }
        }catch(Exception e){
            loghr.writeLog("Exception--"+e.getMessage());
        }
        return reslutRequestid;
    }
    //单独更新正文
    public void updZw(String table,String zw,String requestid){
        List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
        Map<String,Object> result=new HashMap<String,Object>();
        Object[] obj=new Object[1];
        obj[0]=zw;
        result.put("sql","update  "+table+" set zw=? where requestid="+requestid+"");
        result.put("params",obj);
        list.add(result);
        boolean flag=SqlUtil.executeUpdateListTran("local",list);
        loghr.writeLog("flag--"+flag);
    }
}
